package com.sea.supply.controller;

import com.codecrane.core.controller.UploaderController;
import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.finance.controller.HttpUtils;
import com.sea.physical.entity.CreatePlan;
import com.sea.physical.service.CreatePlanService;
import com.sea.product.service.ChannelService;
import com.sea.supply.entity.Bill;
import com.sea.supply.entity.Buy;
import com.sea.supply.entity.ProduceDetail;
import com.sea.supply.entity.ProducePayment;
import com.sea.supply.service.BillService;
import com.sea.supply.service.BuyService;
import com.sea.supply.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sea/supply/bill")
public class BillController {

    @Autowired
    private BuyService buyService;
    @Autowired
    private BillService billService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private CreatePlanService createPlanService;
    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        //供应商
        model.addAttribute("supplier", supplierService.findByCnd(null));
        return "/sea/supply/bill/main";
    }

    @ResponseBody
    @RequestMapping("/findByRefusedId")
    public List<Bill> findByRefusedId(Integer refusedId) {
        return billService.findByRefusedId(refusedId);
    }
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxReturn deleteByIds(@RequestParam("id") Integer id) {
        AjaxReturn result = Ajax.fail().setMsg("操作失败！");
        if (null!=id && billService.delete(id) >0 ) {
            result.setOk(true).setMsg("删除成功");
        }
        return result;
    }
    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("id",id);
        //供应商
        model.addAttribute("supplier", supplierService.findByCnd(null));
        //渠道
        CreatePlan createPlan=new CreatePlan();
        createPlan.setCreatePlanStatus(0);
        model.addAttribute("createPlan", createPlanService.findByCnd(createPlan));
        return "/sea/supply/bill/add";
    }

    @RequestMapping("/editPage")
    public String editPage(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("bill",billService.findById(id));
        return "/sea/supply/bill/edit";
    }



    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(Bill bill) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != bill && billService.usave(bill) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }


    @RequestMapping("/import")
    @ResponseBody
    public AjaxReturn importClubDo(MultipartFile file) {
        AjaxReturn result = Ajax.fail().setMsg("上传失败！");
        try {
            if (null == file || file.isEmpty()) {
                return result.setMsg("请上传文件！！！");
            } else {
                UploaderController upload=new UploaderController();
                AjaxReturn ajaxReturn = upload.uploadFile(file, null);
                String string = ajaxReturn.getData().toString();
                ajaxReturn.setData(billService.getImgInfo(string));
                return ajaxReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        }
    }


    /**
     * 保存
     * @param bill
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(Bill bill) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            if (null != bill && billService.save(bill) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (DuplicateKeyException e){
            result.setMsg("当前渠道不能重复!");
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }





}
